const axios = require('axios')
const cheerio = require('cheerio')
// var fs = require('fs')

//测试代码
function test(){
	const promise = new Promise((resolve,reject)=>{
		let resultList = [];
		let url = 'https://m.meishij.net/jiankang/pinxue/';
		axios.get(url).then(res=>{
			let $ = cheerio.load(res.data);
			let itemList = $('div.cprankitem_s1');
			itemList.each((index,item)=>{
				let result = {};
				const $item = cheerio.load(item);
				result.title = $item('div.info > strong').text();
				result.href = $item('a').attr('href');
				result.image = $item('div.imgw > img').attr('src');
				resultList.push(result);
			});
			resolve(resultList)
		})
	})
	return promise;
}

// 今日三餐
function getTodayFood(){
	const promise = new Promise((resolve,reject)=>{
		let names = ['早餐','午餐','下午茶','晚餐','夜宵'];
		let resultList = [];
		let url = "https://www.meishij.net/";	//爬取地址
		axios.get(url).then(res=>{
			let $ = cheerio.load(res.data);
			let itemList = $('div.sancan_w > div.swiper-container > div.swiper-wrapper > div.swiper-slide');
			itemList.each((index,item)=>{
				let result = {};
				result.type = names[index];
				let foodList = [];
				const $item = cheerio.load(item);
				$item('a').each((index,item2)=>{
					let $item2 = cheerio.load(item2);
					let food = {}
					food.title = $item2('strong').text();
					food.subtitle = $item2('p').text();
					food.href = $item2('a').attr('href');
					food.image = $item2('div.imgw').attr('style');
					foodList.push(food);
				})
				result.foodList = foodList
				resultList.push(result);
			});
			resolve(resultList);
		})
	})
	return promise;
	
}

//首页推荐菜品列表（新鲜推荐、时令菜肴、肉食精选、健康素食、烘焙）
function homeFoodList(){
	const promise = new Promise((resolve,reject)=>{
		let resultList = [];
		let url = "https://www.meishij.net/";	//爬取地址
		axios.get(url).then(res=>{
			let $ = cheerio.load(res.data);
			let itemList = $('div.list_s1');
			itemList.each((index,item)=>{
				let result = {};
				let foodList = [];
				let $item = cheerio.load(item);
				result.type = $item('div.title_s1').text();
				$item('div.list_s1_content > div.list_s1_item').each((index,item2)=>{
					let $item2 = cheerio.load(item2);
					let food = {};
					food.title = $item2('a.list_s1_item_info > strong').text();
					food.tags = $item2('a.list_s1_item_info > span').text();
					food.href = $item2('a.list_s1_item_info').attr('href');
					food.image = $item2('div.imgw > a').attr('style');
					foodList.push(food);
				})
				result.foodList = foodList;
				resultList.push(result);
			})
			resolve(resultList);
		})
	})
	return promise;
	
}

//搜索菜谱方法
function searchFood(foodName,pageNum){
	const promise = new Promise((resolve,reject)=>{
		let url = "https://so.meishi.cc/?q="+foodName+"&sort=time&page="+pageNum
		let resData = {};
		let foodList = [];
		axios.get(encodeURI(url)).then(res=>{
			let $ = cheerio.load(res.data);
			let total = $('div.search2015_path > span > em').text(); //获取搜索总数
			let itemList = $('div.search2015_cpitem');
			itemList.each((index,item)=>{
				let result = {};
				let $item = cheerio.load(item);
				result.title = $item('div.info > a').text().trim();
				result.href = $item('div.info > a').attr('href');
				result.image = $item('a > img').attr('src');
				foodList.push(result);
			})
			resData.foodList = foodList;
			resData.currentPageNum = Number(pageNum);
			resData.total = Number(total);
			resData.totalPage = Math.ceil(Number(total)/20);
			resolve(resData)
		})
	})
	return promise;
}

//查询菜谱详情
function foodDetail(foodUrl){
	const promise = new Promise((resolve,reject)=>{
		let result = {}
		axios.get(foodUrl).then(res=>{
			let $ = cheerio.load(res.data);
			let coverImage = $('div.recipe_header_c > div.recipe_topimgw > img').attr('src');	//封面图
			let title = $('div.recipe_header_info > h1.recipe_title').text();	//标题
			let info2List = $('div.info2 > div.info2_item');	//信息介绍
			let needFoodList = $('div.recipe_ingredientsw > div.recipe_ingredients');	//主料辅料介绍
			let stepList = $('div.main > div.main_left > div.recipe_step_box > div.recipe_step');	//步骤
			let finishImageList = $('div.recipe_finish_box > div.swiper-container > div.swiper-wrapper > div.swiper-slide');	//轮播图
			//工艺
			let gongyis = [];
			info2List.each((index,item)=>{
				let gongyi = {}
				let $item = cheerio.load(item);
				gongyi.type = $item('em').text();
				gongyi.typeValue = $item('strong').text()
				gongyis.push(gongyi);
			})
			//食材
			let neetFoods = [];
			needFoodList.each((index,item)=>{
				let $item = cheerio.load(item);
				let needFood = {};
				let needList = [];
				$item('div.right > strong').each((index,item2)=>{
					let $item2 = cheerio.load(item2);
					needList.push($item2.text())
				})
				needFood.type = $item('div.left > strong').text();
				needFood.needList = needList;
				neetFoods.push(needFood);
			});
			//步骤
			let steps = [];
			stepList.each((index,item)=>{
				let $item = cheerio.load(item);
				let stepNum = $item('div.recipe_step_num > p').text();
				let stepImage = $item('div.step_content > img').attr('src');
				let stepDescribe = $item('div.step_content > p').text();
				let step = {};
				step.stepNum = stepNum;
				step.stepImage = stepImage;
				step.stepDescribe = stepDescribe;
				steps.push(step);
			});
			//轮播图
			finishImages = [];
			finishImageList.each((index,item)=>{
				let $item = cheerio.load(item);
				finishImages.push($item('img').attr('src'));
			})
			//封装结果
			result.title = title;
			result.coverImage = coverImage;
			result.gongyis = gongyis;
			result.neetFoods = neetFoods;
			result.steps = steps;
			result.finishImages = finishImages;
			resolve(result)
		})
	})
	return promise;
}

// foodDetail("http://www.meishij.net/zuofa/youmendaxia_184.html");
module.exports = {
	test,getTodayFood,homeFoodList,searchFood,foodDetail
}
